package com.mf.morefun.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.mf.morefun.R;
import com.zhcx.zhcxlibrary.utils.StringUtils;


/**
 * ProjectName：BranchWisdomBus
 * PackageName：com.zhcx.wisdombus.widget
 * description:
 * Date：2016/9/18 15:54
 * Author：Steven
 * ClassName:ShowRoundProcessDialog
 **/
public class RoundProcessDialog {
    private Dialog customProgressDialog;
    private Context context = null;

    public RoundProcessDialog(Context context, String title) {
        this.context = context;
        this.customProgressDialog = new Dialog(context, R.style.CustomProgressDialog);
        this.customProgressDialog.setContentView(R.layout.customprogressdialog);
//		this.customProgressDialog.getWindow().getAttributes().gravity = (Gravity.TOP|Gravity.LEFT);
        this.customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
        this.customProgressDialog.setCanceledOnTouchOutside(false);
        this.customProgressDialog.setCancelable(false);
        if (tvMsg != null) {
            if (!StringUtils.isEmpty(title)) {
                tvMsg.setText(title);
            } else {
                tvMsg.setText("加载中...");
            }
        }
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
//        ImageView imageView = (ImageView) customProgressDialog
//                .findViewById(R.id.loadingImageView);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView
//                .getBackground();
//        animationDrawable.start();
    }

    /**
     * [Summary] setMessage 提示内容
     *
     * @param strMessage
     * @return
     */
    public Dialog setMessage(String strMessage) {
        try {
            TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
            if (tvMsg != null) {
                tvMsg.setText(strMessage);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return customProgressDialog;
    }

    public void show() {
        try {
            if (!isShow()) {
                if (null != customProgressDialog) {
                    customProgressDialog.show();
                }
                if (customProgressDialog == null) {
                    return;
                }
                ImageView imageView = (ImageView) customProgressDialog
                        .findViewById(R.id.loadingImageView);
                AnimationDrawable animationDrawable = (AnimationDrawable) imageView
                        .getBackground();
                animationDrawable.start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void cancel() {
        try {
            if (isShow()) {
                customProgressDialog.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean isShow() {
        boolean isShow = false;
        try {
            if (customProgressDialog.isShowing()) {
                isShow = true;
            } else {
                isShow = false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return isShow;
    }
}
